package  recycleTest
{
	import asunit.asserts.*;
	import flash.display.Sprite;
	import flash.errors.IllegalOperationError;
	import util.boyaa.recycle.IRecyclable;
	import util.boyaa.recycle.NormalRecycleFactory;
	import util.boyaa.recycle.Recycleable;
	import util.boyaa.recycle.RecycleableVars;
	import util.boyaa.recycle.RecycleFactory;
	
	/**
	 * ...
	 * @author henrylee   Any question, pls contact me. henrylee_sz@qq.com.
	 */
	public final class NormalTest 
	{
		
		public function NormalTest() 
		{
			
		}
		
		private var instance:NormalRecycleFactory;

		[Before]
		public function setUp():void
		{
			instance = new NormalRecycleFactory();
		}
		

		[After]
		public function tearDown():void
		{
			instance.cleanup();
			instance = null;
		}
		
		
		[Test]
		public function testInstantiated():void
		{
			//尝试实例化抽象类会抛出一个错误
			assertThrows(IllegalOperationError, function():void { new RecycleFactory() } );
			assertTrue("RecycleFactory instantiated", instance is NormalRecycleFactory);
			assertNull("target is null by default", instance.getInstanceList(Recycleable));
		}
		
		[Test]
		public function getRecycleableTest():void
		{
			const recycleable: * = instance.getRecyclable(Recycleable);
			assertTrue("getRecyclable() return IRecyclable: ", recycleable is IRecyclable);
			assertEquals(RecycleableVars.AWAKE, recycleable.state);
		}
		
		
		[Test]
		public function recycleableTest():void
		{
			const recycleable: Recycleable = instance.getRecyclable(Recycleable) as Recycleable;
			recycleable.recycle();
			assertEquals(RecycleableVars.RECYCLE, recycleable.state);
			var arr: Vector.<IRecyclable> = instance.getInstanceList(Recycleable);
			assertTrue(arr != null && arr.length == 1);
			recycleable.recycle();
			assertEquals(RecycleableVars.RECYCLE, recycleable.state);
			arr = instance.getInstanceList(Recycleable);
			assertTrue(arr != null && arr.length == 1);
			instance.cleanup();
		}
		
		
		[Test]
		public function recycleableMaxLimitTest():void
		{
			const recycleable1: Recycleable = instance.getRecyclable(Recycleable) as Recycleable;
			const recycleable2: Recycleable = instance.getRecyclable(Recycleable) as Recycleable;
			const recycleable3: Recycleable = instance.getRecyclable(Recycleable) as Recycleable;
			const recycleable4: Recycleable = instance.getRecyclable(Recycleable) as Recycleable;
			const recycleable5: Recycleable = instance.getRecyclable(Recycleable) as Recycleable;
			const recycleable6: Recycleable = instance.getRecyclable(Recycleable) as Recycleable;
			const recycleable7: Recycleable = instance.getRecyclable(Recycleable) as Recycleable;
			recycleable1.recycle();
			recycleable2.recycle();
			recycleable3.recycle();
			recycleable4.recycle();
			recycleable5.recycle();
			recycleable6.recycle();
			recycleable7.recycle();
			
			//测试最多缓存对象
			var arr: Vector.<IRecyclable> = instance.getInstanceList(Recycleable);
			assertTrue(arr != null && arr.length == instance.maxLimit);
			assertEquals(RecycleableVars.DISPOSE, recycleable6.state);
			assertEquals(RecycleableVars.RECYCLE, recycleable5.state);
			
			//测试重复利用
			const recycleable8: Recycleable = instance.getRecyclable(Recycleable) as Recycleable;
			assertEquals(recycleable5, recycleable8);
			assertEquals(RecycleableVars.AWAKE, recycleable5.state);
			assertTrue(arr != null && (arr.length == instance.maxLimit - 1));
			recycleable6.recycle();
			recycleable5.awake();
			recycleable5.recycle();
			var recycleable9: Recycleable = instance.getRecyclable(Recycleable) as Recycleable;
			recycleable9 = instance.getRecyclable(Recycleable) as Recycleable;
			recycleable9 = instance.getRecyclable(Recycleable) as Recycleable;
			recycleable9 = instance.getRecyclable(Recycleable) as Recycleable;
			recycleable9 = instance.getRecyclable(Recycleable) as Recycleable;
			assertTrue(arr != null && arr.length == 0);
			
			//测试清除所有缓存对象
			recycleable9.recycle();
			instance.cleanup();
			arr = instance.getInstanceList(Recycleable);
			assertTrue(arr != null && arr.length == 0);
		}
		
		
		[Test]
		public function recycleInvalid():void
		{
			const recycleable10: Recycleable = instance.getRecyclable(Recycleable) as Recycleable;
			instance.recycle(recycleable10);//不是合法的操作方式
			var arr: Vector.<IRecyclable> = instance.getInstanceList(Recycleable);
			assertTrue(arr == null);
		}
		
	}

}